package com.example.baseweather.ui.view.lyric;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.baseweather.R;
import com.example.baseweather.ui.view.lyric.bean.LyricBean;
import com.example.baseweather.ui.view.lyric.bean.LyricContentBean;
import com.example.baseweather.ui.view.lyric.util.LyricIUtil;

import java.io.File;
import java.util.List;
import java.util.Optional;

public class LyricView extends View {

    private TextPaint commonPaint;
    private TextPaint playPaint;
    private int commonTextSize = 40;
    private int currentTextSize = 45;
    private int width;
    private int height;
    private LyricBean lyricBean;
    private int currentAnimateValue;
    private int currentHighLightIndex = -1;
    private int offsetX = 16;
    private int offsetY = 16;


    public LyricView(Context context) {
        super(context);
        init(context);
    }

    public LyricView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LyricView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public LyricView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        commonPaint = new TextPaint();
        commonPaint.setTextSize(commonTextSize);
        commonPaint.setColor(Color.BLACK);
        playPaint = new TextPaint();
        playPaint.setTextSize(currentTextSize);
        playPaint.setColor(getResources().getColor(R.color.blue_200, getContext().getTheme()));
    }

    public void setLyricFile(File lyricFile) {
        if (lyricFile.getName().endsWith(".lrc")) {
            lyricBean = LyricIUtil.getLyricData(lyricFile);
        } else {
            throw new IllegalArgumentException("当前设置的不是歌词文件!");
        }
    }


    @Override
    protected void onDraw(Canvas canvas) {
        width = getWidth();
        height = getHeight();
        if (lyricBean == null) return;
        drawText(canvas);
    }

    private void drawText(Canvas canvas) {
        final List<LyricContentBean> lyricContentList = lyricBean.getLyricContentList();
        for (int i = 0; i < lyricContentList.size(); i++) {
            final String lyricContent1 = lyricContentList.get(i).getLyricContent();
            // 保存当前画布状态
            if (i < currentHighLightIndex) {
                int realIndex = i - currentHighLightIndex;
                canvas.drawText(lyricContent1, offsetX, realIndex * (commonTextSize + offsetY),
                        commonPaint);
            } else if (i == currentHighLightIndex) {
                drawHighLightText(canvas, getHighLightString(i));
            } else {
                int realIndex = i - currentHighLightIndex;
                canvas.drawText(lyricContent1, offsetX, (2 + realIndex) * (commonTextSize + offsetY),
                        commonPaint);
            }
        }
    }


    private void drawHighLightText(Canvas canvas, @Nullable String lyricContent) {
        if (!Optional.ofNullable(lyricContent).isPresent()) return;
        for (int i = 0; i < lyricContent.length(); i++) {
            if (i <= currentAnimateValue) {
                canvas.drawText(String.valueOf(lyricContent.charAt(i)), offsetX + i * currentTextSize, (commonTextSize + offsetY) * 2
                        , playPaint);
            } else {
                canvas.drawText(String.valueOf(lyricContent.charAt(i)), offsetX + i * currentTextSize, (commonTextSize + offsetY) * 2
                        , commonPaint);
            }
        }
    }

    private void startTextAnimation(@Nullable final String drawText, long duration) {
        System.out.println(drawText);
        if (!Optional.ofNullable(drawText).isPresent()) return;
        final ValueAnimator valueAnimator = ValueAnimator.ofInt(0, drawText.length());
        valueAnimator.setDuration(duration);
        valueAnimator.addUpdateListener(animation -> {
            currentAnimateValue = (int) animation.getAnimatedValue();
            invalidate(); // Trigger onDraw to redraw the view
        });
        //切换下一行
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                if (currentHighLightIndex < lyricBean.getLyricContentList().size() - 1) {
                    currentHighLightIndex++;
                    startTextAnimation(getHighLightString(currentHighLightIndex), duration);
                }
            }
        });
        valueAnimator.start();
    }


    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        this.currentHighLightIndex = 0;
        startTextAnimation(getHighLightString(this.currentHighLightIndex), 1000);
    }


    private String getHighLightString(int highIndex) {
        if (lyricBean == null) return null;
        return lyricBean.getLyricContentList().get(highIndex)
                .getLyricContent();
    }
}